<template>
  <div class="allocate-in">
    <el-card shadow="never" class="search-card">
      <div class="search-bar">
        <!-- 第一行 -->
        <div class="search-row">
          <el-input v-model="allocateCode" placeholder="调拨单号" class="search-item" clearable />
          <el-input v-model="allocatePerson" placeholder="调拨人" class="search-item" clearable />
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD"
            class="search-item"
          />
        </div>

        <!-- 第二行 -->
        <div class="search-row">
          <el-input v-model="productCode" placeholder="商品编码" class="search-item" clearable />
          <el-input v-model="productName" placeholder="商品名称" class="search-item" clearable />
          <el-select v-model="warmzone" placeholder="温区" class="search-item" clearable>
            <el-option label="常温" value="常温" />
            <el-option label="冷藏" value="冷藏" />
            <el-option label="冷冻" value="冷冻" />
          </el-select>
        </div>

        <!-- 第三行 -->
        <div class="search-row">
          <el-input v-model="outName" placeholder="调出仓库" class="search-item" clearable />
          <el-input v-model="inName" placeholder="调入仓库" class="search-item" clearable />
        </div>

        <!-- 操作按钮 -->
        <div class="search-row search-actions">
          <el-button type="primary" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </div>
      </div>
    </el-card>

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="序号" width="60" />
      <el-table-column prop="allocateInCode" label="调拨单号" width="160" />
      <el-table-column prop="tsallocateInCode" label="调拨单号" width="160" />
      <el-table-column prop="allocateName" label="调拨人" width="120" />
      <el-table-column prop="inTime" label="入库时间" width="160" />
      <el-table-column prop="sendTime" label="发送时间" width="160" />
      <el-table-column prop="outName" label="调出仓库" width="120" />
      <el-table-column prop="inName" label="调入仓库" width="120" />
      <el-table-column prop="warmzone" label="温区" width="100" />
      <el-table-column prop="num" label="商品总数" width="140" />
      <el-table-column prop="num" label="数量" width="100" />
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button v-if="!row.status" type="text" @click="handleHarvest(row)">收获</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="page"
      v-model:page-size="pageSize"
      :total="total"
      layout="prev, pager, next, jumper, ->, total"
      style="margin-top: 20px; text-align: right;"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { ref, onMounted } from "vue";
import { allcoateIn,allcoateInOperate } from "@/api/allocate/allocate.js";

const allocateCode = ref("");
const allocatePerson = ref("");
const dateRange = ref([]);
const productCode = ref("");
const productName = ref("");
const warmzone = ref("");
const outName = ref("");
const inName = ref("");

const tableData = ref([]);
const page = ref(1);
const pageSize = ref(2);
const total = ref(0);

async function handleHarvest(row) {
  let res = await allcoateInOperate({id:row.id,status:1});
  if (res.code === 200) {
    ElMessage.success("操作成功");
    init();
  } else {
    ElMessage.error("操作失败：" + (res.msg || "未知错误"));
  }
}

async function init() {
  let queryParams = {
    allocateInCode: allocateCode.value,
    allocateName: allocatePerson.value,
    startDate: dateRange.value?.[0] || "",
    endDate: dateRange.value?.[1] || "",
    productCode: productCode.value,
    productName: productName.value,
    warmzone: warmzone.value,
    outName: outName.value,
    inName: inName.value,
    page: page.value,
    pageSize: pageSize.value
  };
  try {
    let res = await allcoateIn(queryParams);
    if (res.code === 200) {
      tableData.value = res.data.list || [];
      total.value = res.data.total || 0;
    } else {
      ElMessage.error("查询失败：" + (res.msg || "未知错误"));
    }
  } catch (error) {
    ElMessage.error("网络错误，请稍后重试");
  }
}

function search() {
  page.value = 1;
  init();
}

function reset() {
  allocateCode.value = "";
  allocatePerson.value = "";
  dateRange.value = [];
  productCode.value = "";
  productName.value = "";
  warmzone.value = "";
  outName.value = "";
  inName.value = "";
  page.value = 1;
  init();
}

function handlePageChange(newPage) {
  page.value = newPage;
  init();
}

onMounted(() => {
  init();
});
</script>

<style scoped>
.search-card {
  padding: 12px 16px;
  margin-bottom: 15px;
  border-radius: 8px;
}
.search-bar {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.search-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}
.search-item {
  flex: 1;
  min-width: 180px;
}
.search-actions {
  justify-content: flex-end;
  gap: 16px;
}
</style>